#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, sys
from thread import start_new_thread

sys.path.append(os.getcwd()+"/lib")

try:
    import wx
    import database_fetcher, debug, savedebug
    if os.name=="nt": from PTWin32 import *
    elif os.name=="posix": from PTPosix import *
    
except ImportError,e: print e

fetcher=database_fetcher.app # I don't want big names

# Create Dictionary with Widgets

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        try:
            
            kwds["style"] = (wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN | wx.RESIZE_BORDER)
            wx.Frame.__init__(self, *args, **kwds)

            self.path=os.getcwd()
            self.icone=wx.Bitmap(self.path+os.sep+"WataPlayah.ico", wx.BITMAP_TYPE_ANY)


            self.MainNotebook = wx.Notebook(self, -1, style=0)      
            self.MainNotebook_Credits = wx.Panel(self.MainNotebook, -1)
            self.MainNotebook_rGR = wx.Panel(self.MainNotebook, -1)
            self.MainNotebook_rPT = wx.Panel(self.MainNotebook, -1) 
            self.MainNotebook_TVCtry = wx.Panel(self.MainNotebook, -1)
            self.MainNotebook_TVCat = wx.Panel(self.MainNotebook, -1)

            self.widgets=[{},{},{},{}]
            ########## TV By Cat Pane ###########
            self.widgets[0]["LstBox_1"] = wx.ListBox(self.MainNotebook_TVCat, -1, choices=[], style=wx.LB_SORT)
            self.widgets[0]["LstBox_2"] = wx.ListBox(self.MainNotebook_TVCat, -1, choices=[], style=wx.LB_SORT)
            self.widgets[0]["ChnlLabel"] = wx.StaticText(self.MainNotebook_TVCat, -1, ChnlLabel+'\n')
            self.widgets[0]["ChnlImg"] = wx.StaticBitmap(self.MainNotebook_TVCat, -1, self.icone)
            self.widgets[0]["PlayBtn"] = wx.Button(self.MainNotebook_TVCat, -1, PlayBtnTxt)
            self.widgets[0]["CreditsLabel"] = wx.StaticText(self.MainNotebook_TVCat, -1, ShtCds)
            ########## TV By Ctry Pane ###########
            self.widgets[1]["LstBox_1"] = wx.ListBox(self.MainNotebook_TVCtry, -1, choices=[], style=wx.LB_SORT)
            self.widgets[1]["LstBox_2"] = wx.ListBox(self.MainNotebook_TVCtry, -1, choices=[], style=wx.LB_SORT)
            self.widgets[1]["ChnlLabel"] = wx.StaticText(self.MainNotebook_TVCtry, -1, ChnlLabel+'\n')
            self.widgets[1]["PlayBtn"] = wx.Button(self.MainNotebook_TVCtry, -1, PlayBtnTxt)
            self.widgets[1]["ChnlImg"] = wx.StaticBitmap(self.MainNotebook_TVCtry, -1, self.icone)
            self.widgets[1]["CreditsLabel"] = wx.StaticText(self.MainNotebook_TVCtry, -1, ShtCds)
            ########## Portugal Radio Pane ###########
            self.widgets[2]["LstBox_1"] = wx.ListBox(self.MainNotebook_rPT, -1, choices=[], style=wx.LB_SORT)
            self.widgets[2]["LstBox_2"] = wx.ListBox(self.MainNotebook_rPT, -1, choices=[], style=wx.LB_SORT)
            self.widgets[2]["ChnlLabel"] = wx.StaticText(self.MainNotebook_rPT, -1, ChnlLabel+'\n')
            self.widgets[2]["PlayBtn"] = wx.Button(self.MainNotebook_rPT, -1, PlayBtnTxt)
            self.widgets[2]["ChnlImg"] = wx.StaticBitmap(self.MainNotebook_rPT, -1, self.icone)
            self.widgets[2]["CreditsLabel"] = wx.StaticText(self.MainNotebook_rPT, -1, ShtCds)
            ########## Greece Radio Pane ###########
            self.widgets[3]["LstBox_1"] = wx.ListBox(self.MainNotebook_rGR, -1, choices=[], style=wx.LB_SORT)
            self.widgets[3]["LstBox_2"] = wx.ListBox(self.MainNotebook_rGR, -1, choices=[], style=wx.LB_SORT)
            self.widgets[3]["ChnlLabel"] = wx.StaticText(self.MainNotebook_rGR, -1, ChnlLabel+'\n')
            self.widgets[3]["PlayBtn"] = wx.Button(self.MainNotebook_rGR, -1, PlayBtnTxt)
            self.widgets[3]["ChnlImg"] = wx.StaticBitmap(self.MainNotebook_rGR, -1, self.icone)
            self.widgets[3]["CreditsLabel"] = wx.StaticText(self.MainNotebook_rGR, -1, ShtCds)
            ########## Settings/Credits Pane ###########
            self.Intrs_Label = wx.StaticText(self.MainNotebook_Credits, -1, IntrTxt)
            self.PlayerTxtCtrl = wx.TextCtrl(self.MainNotebook_Credits, -1, "")
            self.GetPlayerBtn = wx.Button(self.MainNotebook_Credits, -1, GetPlayerBtnTxt)
            self.Credits_Label = wx.StaticText(self.MainNotebook_Credits, -1, FullCds)
            self.GetPlayerBtn.Bind(wx.EVT_BUTTON, self.Get_Player)
            
            ################### BINDINGS SECTION ###################
            for a in range(0,4): self.widgets[a]["LstBox_1"].Bind(wx.EVT_LISTBOX, self.SectionSelection)
            for a in range(0,4): self.widgets[a]["LstBox_2"].Bind(wx.EVT_LISTBOX, self.ChannelSelection)
            for a in range(0,4): self.widgets[a]["PlayBtn"].Bind(wx.EVT_BUTTON, self.Play)
            ############## FINISH OF BINDINGS SECTION ##############        
        
            self.__set_properties()
            self.__do_layout()
            self.load_sections()

            ########### Section and Channel Selection Functions ###########
        except:
            erro=debug.debug()
            savedebug.savedebug(erro)
            
    def SetFetcher(self):
        try:
                
            fetcher.reset_switch()
            if actual==0: fetcher.set_tv_by_category()
            elif actual==1: fetcher.set_tv_by_country()
            elif actual==2: fetcher.set_radio_portugal()
            elif actual==3: fetcher.set_radio_greece()
        except:
            erro=debug.debug()
            savedebug.savedebug(erro)

    def SectionSelection(self,event):
        try:
                
            global actual
            actual=self.MainNotebook.GetSelection()
            self.SetFetcher()
            
            section = self.widgets[actual]["LstBox_1"].GetStringSelection()

            global channels_list
            channels_list = fetcher.list_cat(section)
            
            if self.Check_AdultTV(section):
                    self.widgets[actual]["LstBox_2"].Clear()
                    for i in channels_list: self.widgets[actual]["LstBox_2"].Append(i)
            else: self.widgets[actual]["LstBox_2"].Clear()
        except:
            erro=debug.debug()
            savedebug.savedebug(erro)

    def ChannelSelection(self,event):
        try:
                
            global actual
            actual=self.MainNotebook.GetSelection()
            self.SetFetcher()
            
            channel = self.widgets[actual]["LstBox_2"].GetStringSelection()

            global stream
            stream=channels_list[channel]
            
            self.widgets[actual]["ChnlLabel"].SetLabel(label="")
            self.widgets[actual]["ChnlLabel"].SetLabel(label=ChnlLabel + " %s"% channel)
            
            slash=os.sep # Like paths' slashes change on system type and as this is a cross-platform
        except:
            erro=debug.debug()
            savedebug.savedebug(erro)


    def Check_AdultTV(self, section):
        try:
                
            if section=="adult tv":
                    self.age18 = wx.MessageDialog(self,WarnTxt,'MessageDialog',wx.YES_NO | wx.ICON_QUESTION)
                    result = self.age18.ShowModal()
                    self.age18.Destroy() # Not necessary, so DESTROY! THIS IS SPARTAAAAAAAAAAA! <- rolando2424's fav expression
            else: result=5103

            if result==5103: return True
            else: return False
        except:
            erro=debug.debug()
            savedebug.savedebug(erro)


    def Play(self,event):
        try:    
            if os.name == "nt": # nasty stuff. blame it on windows...
                amb=os.environ['PROGRAMFILES'] # To know default programs directory
                player=amb+"\\Windows Media Player\\wmplayer.exe "
                playerfix='"'+player+'" '+stream
                try:
                    import win32api
                    win32api.WinExec(playerfix, 1)
                except ImportError:
                    os.system(playerfix)
                            
            else: # *nix like systems
                    try: player=open("player.wata",'r').readlines()[0][0:-1] # Same up
                    except:
                            print "player config file not found!"                   
                            open("player.wata",'w').write("mplayer\nC:\Programas\Windows Media Player\wmp.exe\n")
                            player=open("player.wata",'r').readlines()[0][0:-1]
                    stuff = (player + " " + stream,)
                    if stream != "": start_new_thread(os.system,stuff) # If stream, Use a new thread not to break the program
        except:
            erro=debug.debug()
            savedebug.savedebug(erro)

    def Get_Player(self,event):
        try:
                
            p=self.PlayerTxtCtrl.GetValue() # Get what's written in the TxtCtrl
            if p=="": pass
            else:
                    try: players=open("player.wata",'r').readlines() # Try to open config file
                    except: players=["mplayer","C:\Programas\Windows Media Player\wmp.exe"] # Not? DEFAULT BITCH
                    if os.name == "nt": players[1]=p # Win32 Player 2nd in Array
                    else: players[0]=p # *nix like systems Player 1st in Array
                    open("player.wata",'w').write(players[0]+"\n"+players[1]) # So much work, so WRITE BITCH
        except:
            erro=debug.debug()
            savedebug.savedebug(erro)


    ########### Load Sections in LstBoxes_1 ##########
    def load_sections(self):
        try:
                
            fetcher.set_tv_by_category()
            self.tvcat=fetcher.cats()
            for i in self.tvcat: self.widgets[0]["LstBox_1"].Append(i)

            fetcher.reset_switch()
            fetcher.set_tv_by_country()
            self.tvctry=fetcher.cats()
            for i in self.tvctry: self.widgets[1]["LstBox_1"].Append(i)

            fetcher.reset_switch()
            fetcher.set_radio_portugal()
            self.rtuga=fetcher.cats()
            for i in self.rtuga: self.widgets[2]["LstBox_1"].Append(i)

            fetcher.reset_switch()
            fetcher.set_radio_greece()
            self.rgreece=fetcher.cats()
            for i in self.rgreece: self.widgets[3]["LstBox_1"].Append(i)
            fetcher.reset_switch()

        ########## Nasty wxGlade Stuff ##########
        except:
            erro=debug.debug()
            savedebug.savedebug(erro)


    def __set_properties(self):
        try:
                
            self.SetTitle("Wataplayah")
            self.wicone=wx.Icon(name='WataPlayah.ico', type=wx.BITMAP_TYPE_ICO)
            self.SetIcon(self.wicone)
            self.SetSize((490, 311))
            self.widgets[0]["LstBox_1"].SetMinSize((140, 270))
            self.widgets[0]["LstBox_2"].SetMinSize((140, 270))
            self.widgets[0]["ChnlLabel"].SetForegroundColour(wx.Colour(255, 127, 0))
            self.widgets[0]["ChnlLabel"].SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[0]["PlayBtn"].SetMinSize((75, 30))
            self.widgets[0]["PlayBtn"].SetForegroundColour(wx.Colour(255, 0, 0))
            self.widgets[0]["PlayBtn"].SetFont(wx.Font(10, wx.NORMAL, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[0]["CreditsLabel"].SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[1]["LstBox_1"].SetMinSize((140, 270))
            self.widgets[1]["LstBox_2"].SetMinSize((140, 270))
            self.widgets[1]["ChnlLabel"].SetForegroundColour(wx.Colour(255, 127, 0))
            self.widgets[1]["ChnlLabel"].SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[1]["PlayBtn"].SetMinSize((75, 30))
            self.widgets[1]["PlayBtn"].SetForegroundColour(wx.Colour(255, 0, 0))
            self.widgets[1]["PlayBtn"].SetFont(wx.Font(10, wx.NORMAL, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[1]["CreditsLabel"].SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[2]["LstBox_1"].SetMinSize((140, 270))
            self.widgets[2]["LstBox_2"].SetMinSize((140, 270))
            self.widgets[2]["ChnlLabel"].SetForegroundColour(wx.Colour(255, 127, 0))
            self.widgets[2]["ChnlLabel"].SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[2]["PlayBtn"].SetMinSize((75, 30))
            self.widgets[2]["PlayBtn"].SetForegroundColour(wx.Colour(255, 0, 0))
            self.widgets[2]["PlayBtn"].SetFont(wx.Font(10, wx.NORMAL, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[2]["CreditsLabel"].SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[3]["LstBox_1"].SetMinSize((140, 270))
            self.widgets[3]["LstBox_2"].SetMinSize((140, 270))
            self.widgets[3]["ChnlLabel"].SetForegroundColour(wx.Colour(255, 127, 0))
            self.widgets[3]["ChnlLabel"].SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[3]["PlayBtn"].SetMinSize((75, 30))
            self.widgets[3]["PlayBtn"].SetForegroundColour(wx.Colour(255, 0, 0))
            self.widgets[3]["PlayBtn"].SetFont(wx.Font(10, wx.NORMAL, wx.NORMAL, wx.BOLD, 0, ""))
            self.widgets[3]["CreditsLabel"].SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
            self.Intrs_Label.SetFont(wx.Font(9, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
            self.GetPlayerBtn.SetFont(wx.Font(9, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
            self.Credits_Label.SetForegroundColour(wx.Colour(0, 20, 255))
            self.Credits_Label.SetFont(wx.Font(8, wx.MODERN, wx.NORMAL, wx.BOLD, 0, ""))
        except:
            erro=debug.debug()
            savedebug.savedebug(erro)


    def __do_layout(self):
        try:
                
            MainBigSizer = wx.BoxSizer(wx.VERTICAL)
            SettingsCredits_MainSizer = wx.BoxSizer(wx.HORIZONTAL)
            Config_Sizer = wx.BoxSizer(wx.VERTICAL)
            rGR_Sizer = wx.BoxSizer(wx.HORIZONTAL)
            rGRSideSizer = wx.BoxSizer(wx.VERTICAL)
            rPT_Sizer = wx.BoxSizer(wx.HORIZONTAL)
            rPTSideSizer = wx.BoxSizer(wx.VERTICAL)
            TVCtry_Sizer = wx.BoxSizer(wx.HORIZONTAL)
            TVCtrySideSizer = wx.BoxSizer(wx.VERTICAL)
            TVCat_Sizer = wx.BoxSizer(wx.HORIZONTAL)
            TVCatSideSizer = wx.BoxSizer(wx.VERTICAL)
            TVCat_Sizer.Add(self.widgets[0]["LstBox_1"], 0, wx.EXPAND, 0)
            TVCat_Sizer.Add((7, 20), 0, wx.EXPAND, 0)
            TVCat_Sizer.Add(self.widgets[0]["LstBox_2"], 0, wx.EXPAND, 0)
            TVCat_Sizer.Add((10, 40), 0, 0, 0)
            TVCatSideSizer.Add((20, 10), 0, wx.EXPAND, 0)
            TVCatSideSizer.Add(self.widgets[0]["ChnlLabel"], 0, wx.EXPAND, 0)
            TVCatSideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            TVCatSideSizer.Add(self.widgets[0]["ChnlImg"], 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
            TVCatSideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            TVCatSideSizer.Add(self.widgets[0]["PlayBtn"], 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
            TVCatSideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            TVCatSideSizer.Add(self.widgets[0]["CreditsLabel"], 0, wx.EXPAND, 0)
            TVCat_Sizer.Add(TVCatSideSizer, 1, wx.EXPAND, 0)
            self.MainNotebook_TVCat.SetSizer(TVCat_Sizer)
            TVCtry_Sizer.Add(self.widgets[1]["LstBox_1"], 0, wx.EXPAND, 0)
            TVCtry_Sizer.Add((7, 20), 0, wx.EXPAND, 0)
            TVCtry_Sizer.Add(self.widgets[1]["LstBox_2"], 0, wx.EXPAND, 0)
            TVCtry_Sizer.Add((10, 40), 0, 0, 0)
            TVCtrySideSizer.Add((20, 10), 0, wx.EXPAND, 0)
            TVCtrySideSizer.Add(self.widgets[1]["ChnlLabel"], 0, wx.EXPAND, 0)
            TVCtrySideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            TVCtrySideSizer.Add(self.widgets[1]["ChnlImg"], 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
            TVCtrySideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            TVCtrySideSizer.Add(self.widgets[1]["PlayBtn"], 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
            TVCtrySideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            TVCtrySideSizer.Add(self.widgets[1]["CreditsLabel"], 0, wx.EXPAND, 0)
            TVCtry_Sizer.Add(TVCtrySideSizer, 1, wx.EXPAND, 0)
            self.MainNotebook_TVCtry.SetSizer(TVCtry_Sizer)
            rPT_Sizer.Add(self.widgets[2]["LstBox_1"], 0, wx.EXPAND, 0)
            rPT_Sizer.Add((7, 20), 0, wx.EXPAND, 0)
            rPT_Sizer.Add(self.widgets[2]["LstBox_2"], 0, wx.EXPAND, 0)
            rPT_Sizer.Add((10, 40), 0, 0, 0)
            rPTSideSizer.Add((20, 10), 0, wx.EXPAND, 0)
            rPTSideSizer.Add(self.widgets[2]["ChnlLabel"], 0, wx.EXPAND, 0)
            rPTSideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            rPTSideSizer.Add(self.widgets[2]["ChnlImg"], 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
            rPTSideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            rPTSideSizer.Add(self.widgets[2]["PlayBtn"], 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
            rPTSideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            rPTSideSizer.Add(self.widgets[2]["CreditsLabel"], 0, wx.EXPAND, 0)
            rPT_Sizer.Add(rPTSideSizer, 1, wx.EXPAND, 0)
            self.MainNotebook_rPT.SetSizer(rPT_Sizer)
            rGR_Sizer.Add(self.widgets[3]["LstBox_1"], 0, wx.EXPAND, 0)
            rGR_Sizer.Add((7, 20), 0, wx.EXPAND, 0)
            rGR_Sizer.Add(self.widgets[3]["LstBox_2"], 0, wx.EXPAND, 0)
            rGR_Sizer.Add((10, 40), 0, 0, 0)
            rGRSideSizer.Add((20, 10), 0, wx.EXPAND, 0)
            rGRSideSizer.Add(self.widgets[3]["ChnlLabel"], 0, wx.EXPAND, 0)
            rGRSideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            rGRSideSizer.Add(self.widgets[3]["ChnlImg"], 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
            rGRSideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            rGRSideSizer.Add(self.widgets[3]["PlayBtn"], 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
            rGRSideSizer.Add((20, 15), 0, wx.EXPAND, 0)
            rGRSideSizer.Add(self.widgets[3]["CreditsLabel"], 0, wx.EXPAND, 0)
            rGR_Sizer.Add(rGRSideSizer, 1, wx.EXPAND, 0)
            self.MainNotebook_rGR.SetSizer(rGR_Sizer)
            Config_Sizer.Add((5, 25), 0, wx.EXPAND, 0)
            Config_Sizer.Add(self.Intrs_Label, 0, wx.EXPAND, 0)
            Config_Sizer.Add((5, 10), 0, wx.EXPAND, 0)
            Config_Sizer.Add(self.PlayerTxtCtrl, 0, wx.EXPAND, 0)
            Config_Sizer.Add((5, 10), 0, wx.EXPAND, 0)
            Config_Sizer.Add(self.GetPlayerBtn, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
            SettingsCredits_MainSizer.Add(Config_Sizer, 1, wx.EXPAND, 0)
            SettingsCredits_MainSizer.Add((5, 40), 0, wx.EXPAND, 0)
            SettingsCredits_MainSizer.Add(self.Credits_Label, 0, wx.EXPAND, 0)
            self.MainNotebook_Credits.SetSizer(SettingsCredits_MainSizer)
            self.MainNotebook.AddPage(self.MainNotebook_TVCat, Section1)
            self.MainNotebook.AddPage(self.MainNotebook_TVCtry, Section2)
            self.MainNotebook.AddPage(self.MainNotebook_rPT, Section3)
            self.MainNotebook.AddPage(self.MainNotebook_rGR, Section4)
            self.MainNotebook.AddPage(self.MainNotebook_Credits, Section5)
            MainBigSizer.Add(self.MainNotebook, 1, wx.EXPAND, 0)
            self.SetSizer(MainBigSizer)
            self.Layout()
        except:
            erro=debug.debug()
            savedebug.savedebug(erro)


if __name__ == "__main__":
    try:
            
        app = wx.PySimpleApp(0)
        wx.InitAllImageHandlers()
        MainWindow = MyFrame(None, -1, "")
        app.SetTopWindow(MainWindow)
        MainWindow.Show()
        app.MainLoop()
    except:
            erro=debug.debug()
            savedebug.savedebug(erro)

